<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="地区" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['fwzId']" :trigger-change="true" dictCode="tb_jrtx_fwz,township_name,id" placeholder="请选择地区"/>
        </a-form-item>
          
        <a-form-item label="贷款业务号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'loanNumber', validatorRules.loanNumber]" placeholder="请输入贷款业务号码"></a-input>
        </a-form-item>
          
        <a-form-item label="贷款人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'loanName', validatorRules.loanName]" placeholder="请输入贷款人姓名"></a-input>
        </a-form-item>
          
        <a-form-item label="身份证" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'idNumber', validatorRules.idNumber]" placeholder="请输入身份证"></a-input>
        </a-form-item>
          
        <a-form-item label="贷款银行类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['loanBankType']" :trigger-change="true" dictCode="bank_type" placeholder="请选择贷款银行类型"/>
        </a-form-item>
          
        <a-form-item label="贷款银行名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['loanBankName']" :trigger-change="true" dictCode="tb_dict_bank,bank_name,id" placeholder="请选择贷款银行名称"/>
        </a-form-item>
          
        <a-form-item label="贷款金额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'loanMoney', validatorRules.loanMoney]" placeholder="请输入贷款金额（元）"></a-input>
        </a-form-item>
          
        <a-form-item label="借款日" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择借款日" v-decorator="[ 'borrowingDate', validatorRules.borrowingDate]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
          
        <a-form-item label="到期日" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择到期日" v-decorator="[ 'endDate', validatorRules.endDate]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
          
        <a-form-item label="贷款用途" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['loanUse']" :trigger-change="true" dictCode="loan_use" placeholder="请选择贷款用途"/>
        </a-form-item>
          
        <a-form-item label="是否购买贷款保证保险" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['isLoanInsurance']" :trigger-change="true" dictCode="is_loan_insurance" placeholder="请选择是否购买贷款保证保险"/>
        </a-form-item>
          
        <a-form-item label="年利率" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'interestRates', validatorRules.interestRates]" placeholder="请输入年利率"></a-input>
        </a-form-item>
          
        <a-form-item label="贴息利率" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'compensationInterestRate', validatorRules.compensationInterestRate]" placeholder="请输入贴息利率"></a-input>
        </a-form-item>
          
        <a-form-item label="应贴息金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'compensationMoney', validatorRules.compensationMoney]" placeholder="请输入应贴息金额"></a-input>
        </a-form-item>
          
        <a-form-item label="是否参加过信用等级评定" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['isEvaluation']" :trigger-change="true" dictCode="is_evaluation" placeholder="请选择是否参加过信用等级评定"/>
        </a-form-item>
          
        <a-form-item label="贷款类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['loanType']" :trigger-change="true" dictCode="loan_type" placeholder="请选择贷款类型"/>
        </a-form-item>
          
        <a-form-item label="贷款模式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['loanMode']" :trigger-change="true" dictCode="loan_mode" placeholder="请选择贷款模式"/>
        </a-form-item>
          
        
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  
  export default {
    name: "SmallLoanAccountModal",
    components: { 
      JDate,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        fwzId:{},
        loanNumber:{},
        loanName:{},
        idNumber:{},
        loanBankType:{},
        loanBankName:{},
        loanMoney:{},
        borrowingDate:{},
        endDate:{},
        loanUse:{},
        isLoanInsurance:{},
        interestRates:{},
        compensationInterestRate:{},
        compensationMoney:{},
        isEvaluation:{},
        loanType:{},
        loanMode:{},
        },
        url: {
          add: "/loanmanagement/smallLoanAccount/add",
          edit: "/loanmanagement/smallLoanAccount/edit",
        }
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'fwzId','loanNumber','loanName','idNumber','loanBankType','loanBankName','loanMoney','borrowingDate','endDate','loanUse','isLoanInsurance','interestRates','compensationInterestRate','compensationMoney','isEvaluation','loanType','loanMode'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'fwzId','loanNumber','loanName','idNumber','loanBankType','loanBankName','loanMoney','borrowingDate','endDate','loanUse','isLoanInsurance','interestRates','compensationInterestRate','compensationMoney','isEvaluation','loanType','loanMode'))
      }
      
    }
  }
</script>